package editAir.editContent
{
	import _me.Scene_data;
	
	import editAir.buildEdit.EditManage;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.geom.Vector3D;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;

	public class FileCell extends Sprite
	{
		private var titleTxt:TextField;
		private var _filePic:DisplayObject;
		private var _modelPic:DisplayObject;
		private var _folderInfo:MovieClip;
		private var _titleSprite:Sprite;

		private var _modelVO:ModelVO
		private var _backPic:Shape;

		public function FileCell()
		{
			addBacks();
			addPics();
			addTitles();

			addTexts();
			addEvents();

		}

		private function addTitles():void
		{
			_titleSprite=new Sprite;
			_titleSprite.x=20;
			addChild(_titleSprite);

			_modelPic=ModelUiManege.getClass("pic_image_4");
			_modelPic.x=0
			_modelPic.y=2
			_modelPic.visible=false;
			_titleSprite.addChild(_modelPic);


			_filePic=ModelUiManege.getClass("pic_image_wj");
			_filePic.x=0
			_filePic.y=2
			_filePic.visible=false;
			_titleSprite.addChild(_filePic);
		}

		private function addEvents():void
		{
			_folderInfo.addEventListener(MouseEvent.CLICK, onClik)
			_titleSprite.addEventListener(MouseEvent.MOUSE_DOWN, cellMouseDown)
			_titleSprite.addEventListener(MouseEvent.MOUSE_UP, cellMouseUp)
			_titleSprite.addEventListener(MouseEvent.DOUBLE_CLICK,cellDoubleClik)
			_titleSprite.addEventListener(MouseEvent.CLICK,cellClik)
			_titleSprite.doubleClickEnabled =true;
			titleTxt.addEventListener(FocusEvent.FOCUS_OUT,titleTxtFocusOut)
		}
		
		private var canClik:Boolean=true;
		protected function cellClik(event:MouseEvent):void
		{
			if(!canClik)return;
			canClik=false;
			// TODO Auto-generated method stub
			ModelList(parent.parent).selectCell(_modelVO.id);
			
			var c:Array=(EditManage.buildView.selectItem)
			
			
		
			
			var centenPosition:Vector3D=ModelUiManege.fileList.getSelectItemCenten(EditManage.buildView.selectItem)
				
			if(!centenPosition.x)
			{
				canClik=true;
				return;
			}
				
				
			var lastx:Number=Scene_data.focus3D.x
			var lastz:Number=Scene_data.focus3D.z
	 
		  
			if(!centenPosition)return;
			var itime:uint = setInterval(myRepeatingFunction, 10, "Hello", "World");
			var loopNum:int=0
			function myRepeatingFunction():void
			{
				var sc:Number=easeInOut(loopNum, 0, 1, 20)
				Scene_data.focus3D.x=	lastx+(centenPosition.x-lastx)*sc
				Scene_data.focus3D.z=	lastz+(centenPosition.z-lastz)*sc
				loopNum++
				if(loopNum>=20){
					canClik=true;
					clearInterval(itime);  
				}
			}
		}
		private  function easeInOut(t:Number,b:Number,c:Number,d:Number):Number {
			if ((t/=d/2) < 1) {
				return c/2*t*t*t*t + b;
			}
			return -c/2 * ((t-=2)*t*t*t - 2) + b;
		}
		protected function titleTxtFocusOut(event:FocusEvent):void
		{
			_modelVO.name=titleTxt.text.length>0?titleTxt.text:_modelVO.name;
			titleTxt.mouseEnabled=false;
			titleTxt.type = TextFieldType.DYNAMIC;
		}
		
		protected function cellDoubleClik(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			titleTxt.mouseEnabled=true;
			titleTxt.type = TextFieldType.INPUT;
			
	
		}
		
		protected function cellMouseUp(event:MouseEvent):void
		{
			if (_modelVO.filetype == 1)
			{
				ModelUiManege.fileListView.changeModelVoFather(_modelVO.id);
			}

		}

		protected function cellMouseDown(event:MouseEvent):void
		{
			ModelUiManege.fileListView.moveModelvo(_modelVO);
		}

		private function addPics():void
		{
			_folderInfo=ModelUiManege.getClass("mc_folder_info");
			_folderInfo.x=5
			_folderInfo.y=10
			addChild(_folderInfo);



		}

		private function addTexts():void
		{
			var thistextformat:TextFormat=new TextFormat;
			titleTxt= new TextField()
			titleTxt.defaultTextFormat = thistextformat;
			titleTxt.autoSize = TextFieldAutoSize.LEFT;
			titleTxt.htmlText="";
			titleTxt.type = TextFieldType.INPUT;
			titleTxt.type = TextFieldType.DYNAMIC;
			titleTxt.mouseEnabled=false;
			_titleSprite.addChild(titleTxt);
			titleTxt.x=22;
			titleTxt.y=3
				
		

		}

		private function onClik(event:MouseEvent):void
		{
			_modelVO.open=!_modelVO.open;
			ModelList(parent.parent).changePasition();
			
			setSate();
		}

		public function setSate():void
		{
			titleTxt.htmlText=_modelVO.name
			if (_modelVO.open)
			{
				_folderInfo.gotoAndStop(2)
			}
			else
			{
				_folderInfo.gotoAndStop(1)
			}
			if (_modelVO.filetype == 1)
			{
				_filePic.visible=true;
				_folderInfo.visible=true;
			}
			else
			{
				_modelPic.visible=true;
				_folderInfo.visible=false;
			}
			if(_modelVO.select){
				drawBackColor(1);
				
			}else{
				drawBackColor(0);
			}
			if(_modelVO.haveson){
				_folderInfo.visible=true;
			}else{
				_folderInfo.visible=false;
			}
	
		}
		private function drawBackColor(num:int):void
		{
			_backPic.graphics.clear();
			if(num==0){
				_backPic.graphics.clear();
			}
			if(num==1){
				_backPic.graphics.beginFill(0x000000, 0.1);
				_backPic.graphics.drawRect(40, 2,5+ titleTxt.width, 20);
				_backPic.graphics.endFill();
			}
		}

		public function set source(tempVo:ModelVO):void
		{
			_modelVO=tempVo
			if (tempVo)
			{
				setSate();
			}
		}
		private function addBacks():void
		{
			_backPic=new Shape;
			addChild(_backPic);
	
		}
	}
}
